//
//  ZJEmtionView.swift
//  Webo
//
//  Created by 曾静 on 16/7/4.
//  Copyright © 2016年 qq. All rights reserved.
//

import UIKit

let tagMargin = 1001
class ZJEmtionView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setupUI(){
        
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
//在ios9.0之后
           if #available(iOS 9.0, *) {
               addSubview(emotionStackToolBar)
            
            emotionStackToolBar.emotionClourse = {(type:EmotionToolBarType)->() in
                
                let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue - tagMargin)
                self.pictureView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            }
            
            
           } else {
            //通过设置约束的方式创建底的
            addSubview(emotionToolBar)
            
            emotionToolBar.emotionToolClourse = {(type:EmotionToolBarType)->() in
                
                let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue - tagMargin)
                self.pictureView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            }
            
           }

          addSubview(pictureView)
        
        

        
        //设置约束
        if #available(iOS 9.0, *) {
            emotionStackToolBar.snp_makeConstraints { (make) -> Void in
                
                make.leading.trailing.bottom.equalTo(self)
                
                make.height.equalTo(37)
                
                
            }
        } else {
            // Fallback on earlier versions
            emotionToolBar.snp_makeConstraints(closure: { (make) -> Void in
                
                make.leading.trailing.bottom.equalTo(self)
                
                make.height.equalTo(37)
                
            })
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.top.equalTo(self)
            
            
            if #available(iOS 9.0, *) {
                
              make.bottom.equalTo(self.emotionStackToolBar.snp_top)

            }else{
                
               make.bottom.equalTo(self.emotionToolBar.snp_top)
            }
        }
    }
    
    //懒加载控件
    @available(iOS 9.0, *)
    private lazy var emotionStackToolBar:ZJEmotionToolBarView = ZJEmotionToolBarView(frame: CGRectZero)
    
    private lazy var emotionToolBar:ZJEmotionToolBar = ZJEmotionToolBar()
    
    private lazy var pictureView:ZJEmtionPicureView = {
    
      let pictureView = ZJEmtionPicureView()
        
        //因为pictureView的代理可以设置到自己的类里面,但是由于cell现实的组要影响到底部toolBar的选中状态,所以放在里面,省去通知的过程,记住!!!!
      pictureView.delegate = self
        
      return pictureView
    
    
    }()
    
    
       
 

}

extension ZJEmtionView : UICollectionViewDelegate{
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        //在它滚动的时候监听屏幕中的cell的frame那个包含屏幕中心的位置
        let  myCenterX = scrollView.contentOffset.x + SCREENW * 0.5
        
        print(myCenterX)
        //获取屏幕中现实的cell
        let visierbleCellArray = pictureView.visibleCells().sort { (cell1, cell2) -> Bool in
            cell1.frame.origin.x < cell2.frame.origin.x
        }
        
        let cell1 = visierbleCellArray.first!
        
        let cell2 = visierbleCellArray.last!
        
        
        
        
        //如果屏幕滚动了,屏幕当中就有两个cell
        if visierbleCellArray.count == 2{
            
            var indexPath1:NSIndexPath
//            cell1.frame.contains(CGPoint(x: centerX, y: 0))
            if  cell1.frame.contains(CGPoint(x: myCenterX, y: 0)){
                
             
                indexPath1 = pictureView.indexPathForCell(cell1)!
                
//                print(indexPath1.section)
                
                
            }else{
                
                indexPath1 = pictureView.indexPathForCell(cell2)!
                
//                print(indexPath1.section)
            }
            
            if #available(iOS 9.0, *) {
                emotionStackToolBar.changToolBarSelected(indexPath1)
            } else {
                // Fallback on earlier versions
                emotionToolBar.changToolBarSelected(indexPath1)
            }
        }
        
        
        
    }
    
}
